package top.hjcwzx.wrench.mqtt.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.hjcwzx.wrench.mqtt.enums.MessageHandlerStrategyEnum;
import top.hjcwzx.wrench.mqtt.service.core.MqttService;
import top.hjcwzx.wrench.mqtt.service.core.MqttSubscribeRegistrar;
import top.hjcwzx.wrench.mqtt.service.listener.MqttConnectionLostHandler;
import top.hjcwzx.wrench.mqtt.service.publish.MqttEventPublisher;
import top.hjcwzx.wrench.mqtt.service.publish.MqttPublisher;
import top.hjcwzx.wrench.mqtt.strategy.MqttMessageHandler;
import top.hjcwzx.wrench.mqtt.strategy.MqttMessageManager;
import top.hjcwzx.wrench.mqtt.strategy.impl.DirectCallbackStrategy;
import top.hjcwzx.wrench.mqtt.strategy.impl.EventPublishStrategy;
import top.hjcwzx.wrench.mqtt.strategy.impl.MixedStrategy;

import java.util.HashMap;

/**
 *
 * <p><b>类名：</b> MqttConfiguration</p>
 * <p><b>描述：</b> Mqtt 配置</p>
 * <p><b>创建时间：</b> 2025-06-17 15:31</p>
 *
 * @author 胡锦程
 */
@Configuration
@EnableConfigurationProperties(value = {MqttConfigProperties.class})
public class MqttConfiguration {

    /**
     * json解析
     */
    @Bean("kitObjectMapper")
    public ObjectMapper kitObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        return mapper;
    }

    /**
     * mqtt事件发布器
     */
    @Bean("mqttEventPublisher")
    public MqttEventPublisher mqttEventPublisher(ApplicationEventPublisher publisher) {
        return new MqttEventPublisher(publisher);
    }

    /**
     * mqtt消息处理策略管理器
     */
    @Bean("mqttMessageManager")
    public MqttMessageManager mqttMessageManager(MqttEventPublisher mqttEventPublisher) {
        return new MqttMessageManager(new HashMap<String, MqttMessageHandler>() {{
            put(MessageHandlerStrategyEnum.ANNOTATION_CALLBACK.getBeanName(), new DirectCallbackStrategy());
            put(MessageHandlerStrategyEnum.EVENT_PUBLISH.getBeanName(), new EventPublishStrategy(mqttEventPublisher));
            put(MessageHandlerStrategyEnum.MIXED.getBeanName(), new MixedStrategy(mqttEventPublisher));
        }});
    }

    /**
     * mqtt核心服务
     */
    @Bean("mqttService")
    public MqttService mqttService(MqttConfigProperties properties,
                                   MqttEventPublisher publisher,
                                   MqttMessageManager mqttMessageManager,
                                   ObjectMapper kitObjectMapper) {
        return new MqttService(properties, publisher, mqttMessageManager, kitObjectMapper);
    }

    /**
     * 消息注册
     */
    @Bean("mqttSubscribeRegistrar")
    public MqttSubscribeRegistrar mqttSubscribeRegistrar(MqttService mqttService) {
        return new MqttSubscribeRegistrar(mqttService);
    }

    /**
     * 消息发布
     */
    @Bean("mqttPublisher")
    public MqttPublisher mqttPublisher(MqttService mqttService) {
        return mqttService;
    }

    /**
     * 重连处理器
     */
    @Bean("mqttConnectionLostHandler")
    public MqttConnectionLostHandler mqttConnectionLostHandler(MqttConfigProperties properties, MqttService mqttService) {
        return new MqttConnectionLostHandler(mqttService, properties);
    }
}
